﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using CMSMVC4.Areas.Main.Models;
using CMSMVC4.Filters;

namespace CMSMVC4.Areas.Main.Controllers
{
    public class CommentController : Controller
    {
        //
        // GET: /Main/Comment/
        CMSMVC4Entities db = new CMSMVC4Entities();
        CommandSql cm = new CommandSql();

        [ChildActionOnly]
        public ActionResult ListComment(ListCommentModel model)
        {
            if (ModelState.IsValid)
            {
                string index = "1";
                int count = cm.SetCountComment(5, model.id);
                var row = cm.GetDataComment(ref index, model.id);
                ViewBag.id = model.id;
                ViewBag.count = count;
                return PartialView(row);
            }
            return View("Error");
        }

        [HttpPost]
        [OutputCache(Duration = 900, VaryByParam = "id,page", Location = OutputCacheLocation.Client, NoStore = true)]
        public ActionResult CommentNext(CommentNextModel model)
        {
            if (ModelState.IsValid)
            {
                string index = (int.Parse(model.page) + 1).ToString(CultureInfo.InvariantCulture);
                int count = cm.SetCountComment(5, model.id);
                if (int.Parse(index) <= count)
                {
                    var row = cm.GetDataComment(ref index, model.id);
                    if (Request.IsAjaxRequest())
                    {
                        return PartialView(row);
                    }
                    return View(row);
                }
            }
            return JavaScript("");
        }

        [ChildActionOnly]
        public ActionResult SendComment(string id)
        {
            SendCommentModel model = new SendCommentModel();
            model.id = id;
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult SendComment(SendCommentModel model)
        {
            if (ModelState.IsValid)
            {
                var row = new Coment();
                row.Data = model.data;
                row.Email = model.email;
                row.Page = int.Parse(model.id);
                row.Status = CommentStatus.NotConfirm;
                db.Coments.AddObject(row);
                db.SaveChanges();
                //----------send mail------------
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

                string mailto = System.Configuration.ConfigurationManager.AppSettings["EmailTo"];
                message.From = new System.Net.Mail.MailAddress("3dscanvietnam@gmail.com");
                message.To.Add(new System.Net.Mail.MailAddress(mailto));

                var page = db.Pages.FirstOrDefault(x => x.Id == row.Page);
                string urlbody = "http://3dscan.com.vn";
                if (page != null)
                {
                    if (page.Category.Type == MenuType.ThiteBi)
                    {
                        urlbody = "http://3dscan.com.vn/ThietBi/Index/" + page.CategoriesID;
                    }
                    if (page.Category.Type == MenuType.DichVu)
                    {
                        urlbody = "http://3dscan.com.vn/DichVu/BaiViet/" + page.CategoriesID;
                    }
                    if (page.Category.Type == MenuType.TinTuc)
                    {
                        urlbody = "http://3dscan.com.vn/TinTuc/BaiViet/" + page.Id;
                    }
                }
                message.IsBodyHtml = true;
                message.BodyEncoding = Encoding.UTF8;
                message.Subject = "3DSCAN VIET NAM - Comment";
                message.Body = @"<span style='font-weight: bold;'>Email: </span><a href='mailto:" + model.email + "' target='_blank'>" + model.email + @"</a> <br/>
                                 <span style='font-weight: bold;'>Nội Dung Tin Nhắn: </span><span style='color: rgb(173, 19, 19);font-size: 15px;'>" + model.data + @"</span><p/>
                                 <span style='font-weight: bold;'>Link Bài Viết: </span><a href='"+urlbody+@"' target='_blank'>"+urlbody+@"</a><br/>
                                 <span style='font-weight: bold;'>Link Quản Lý: </span><a href='http://3dscan.com.vn/Admin/Comment/NotConfirm' target='_blank'>http://3dscan.com.vn/Admin/Comment/NotConfirm</a>";

                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
                client.Timeout = 15000;
                client.Send(message);
                //-------------------------------
                model.data = null;
                ModelState.AddModelError("", MultiResource.Resource.Comment_success);
            }
            if (Request.IsAjaxRequest())
            {
                return PartialView(model);
            }
            return View(model);
        }
    }
}
